package 将任意对象作为对象监视器;


class Service{
	private String usernameParam = new String();
	private String passwordParam;
	String anyString=new String();//使得线程同步执行
	public void serUsernamePassword(String username,String password)
	{
		try {
		    	
			synchronized(anyString) {
			   
				System.out.println("线程名称为：" + Thread.currentThread().getName()
						+"在"+System.currentTimeMillis()+"进入同步块");
				usernameParam=username;
				Thread.sleep(3000);
				passwordParam=password;
				System.out.println("线程名称为：" + Thread.currentThread().getName()
						+"在"+System.currentTimeMillis()+"离开同步块");
			  
			}
		}
		catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}
class Mythread1 extends Thread{
	private Service task;
	public Mythread1(Service task){
		super();
		this.task=task;
	}
	public void run()
	{
		task.serUsernamePassword("a", "aa");
		
	}
}
class Mythread2 extends Thread{
	private Service task;
	public Mythread2(Service task){
		super();//调用父类构造方法
		this.task=task;
	}
	public void run()
	{
		task.serUsernamePassword("a", "bb");
		
	}
}
public class Test2_29 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Service service=new Service();
		Mythread1 a=new Mythread1(service);
		a.setName("a");
		a.start();
		Mythread1 b=new Mythread1(service);
		b.setName("b");
		b.start();
		
	}

}
